/*
 * Brown, Gregory; Negron, Luz; Noecker, David; Spangler, Michael
 * Pennsylvania State University
 * 2011
 *
 * Copyright (c) 2011, Brown Negron Noecker Spangler
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of Pennsylvania State University nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Config class for use in IST 411 project
 */

package courseScheduler;

import java.io.*;
import java.util.logging.*;

/*
 *
 * Need to finish based on groups input.
 *
 */



/**
 *
 * @author Greg Brown
 */
public class Config {
    /**
     * Constructor with all the fields filled in
     * @param server
     * @param user name
     * @param password
     */
    Config(String server, String user, String pass){
        serverLocation= server;
        userName=user;
        password=pass;
    }
    Config(){
        
    }
    final int INFO=3;
    //ivars
    private String configFileDest=System.getProperty("user.home")+File.separator+"courseScheduler";//Change to whatever path you want
    private String serverLocation;
    private String userName;
    private String password;


    //Start of Gets and Sets
    public void setServerLocation(String server){
        serverLocation=server;
    }
    public String getServerLocation(){
        return(serverLocation);
    }

    public void setUserName(String user){
        userName=user;
    }
    public String getUserName(){
        return(userName);
    }

    public void setPassword(String pass){
        password=pass;
    }
    public String getPassword(){
        return(password);
    }
    
    public String getConfigFileDest(){
        return(configFileDest);
    }
    //End of Gets and Sets





    /**
     * Generates a configuration file
     */
    public void generateConfigFile(){
        File configFile=new File(this.getConfigFileDest());
        boolean pathExists=configFile.exists();
        if(!(pathExists)){
            boolean mkdirSuccess=configFile.mkdir();
            if(mkdirSuccess){
                try {
                    FileWriter outStream = new FileWriter(this.getConfigFileDest()+File.separator+"log");
                    BufferedWriter myConfig= new BufferedWriter(outStream);
                    myConfig.write(this.getServerLocation()+"\n");
                    myConfig.write(this.getUserName()+"\n");
                    myConfig.write(this.getPassword()+"\n");
                    myConfig.close();
                }
                catch (IOException ex) {
                     Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }
    }

    /**
     * Recovers the configuration file if one exists, and stores the information in a
     * array of strings that is used for the login.  If a configuration file doesn't exist it will
     * return null.
     *
     * @return String[]
     */
    public String[] recoverConfigFile() {
        boolean pathExists=new File(this.getConfigFileDest()).exists();
        String[] info=new String[INFO];
        if(pathExists){
            try {
                FileReader inStream = new FileReader(this.getConfigFileDest() + File.separator + "log");
                BufferedReader myConfig=new BufferedReader(inStream);
                try {
                    info[0]=myConfig.readLine();
                    info[1]=myConfig.readLine();
                    info[2]=myConfig.readLine();
                    myConfig.close();
                }
                catch (IOException ex) {
                    Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (FileNotFoundException ex) {
                Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
            info=null;
        }
        return info;
    }
}
